package com.gc.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.gc.business.domain.AnnualInspection;
import com.gc.business.domain.AnnualPlan;
import com.gc.business.domain.ElevatorInfo;
import com.gc.business.service.IAnnualInspectionService;
import com.gc.business.service.IAnnualPlanService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.common.constant.Constants;
import com.gc.common.utils.StringUtils;
import com.gc.framework.web.base.ApiBaseController;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @title 电梯年检相关api接口层
 * @Date 2020/12/01 14:41
 * @author hruiyan
 */
@CrossOrigin
@Controller
@RequestMapping("/api/annual")
public class ApiAnnualInspectionController extends ApiBaseController {

    @Autowired
    private IElevatorInfoService elevatorInfoService;

    @Autowired
    private IAnnualPlanService annualPlanService;

    @Autowired
    private IAnnualInspectionService annualInspectionService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询任务不同状态下的年检列表
     * @param reviewType 0.待启动 1.待确定 2.确定完成
     * @return
     */
    @GetMapping("/selectAnnualInspectionList")
    @ResponseBody
    public Map<String,Object> selectAnnualInspectionList(Integer pageNum,Integer pageSize,String token,String reviewType){
        Long headUserId = getUserId(token);
        SysUser userInfo = userService.selectUserById(headUserId);
        if(!"0".equals(reviewType) && !"1".equals(reviewType) && !"2".equals(reviewType)){
            return error(Constants.UNVERIFY,"reviewType状态有误！");
        }
        if ("0".equals(reviewType)){//待启动  创建时间倒序
            startPage(pageNum,pageSize,"createTime DESC");
        }else if ("1".equals(reviewType)){//执行中  启动时间倒序
            startPage(pageNum,pageSize,"startTime DESC");
        }else if ("2".equals(reviewType)){//已完成  完成时间倒序
            startPage(pageNum,pageSize,"completionTime DESC");
        }
        List<Map<String,Object>>  annualInspectionList = annualInspectionService.selectAnnualInspectionList(userInfo.getAttrId(), reviewType, headUserId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNum",pageNum);
        jsonObject.put("list",annualInspectionList);
        jsonObject.put("total",new PageInfo<>(annualInspectionList).getTotal());
        return success(jsonObject);
    }


    /**
     * 启动电梯年检接口
     * @param token
     * @param inspectionId
     * @return
     */
    @PostMapping("/startAnnualInspection")
    @ResponseBody
    public Map<String,Object> startAnnualInspection(String token,Long inspectionId){
        if(StringUtils.isNull(inspectionId)){
            return error(Constants.UNVERIFY,"缺少必传参数：id !");
        }

        Long userId = getUserId(token);

        AnnualInspection byId = annualInspectionService.getById(inspectionId);

        /**
         * 校验是否被接单
         */
        if(StringUtils.isNotNull(byId.getHeadUserId()) && !byId.getHeadUserId().equals(userId)){
            return error(Constants.UNVERIFY,"该年检已归维保人员负责 ！");
        }


        try {
            SysUser sysUser = userService.selectUserById(userId);

            //修改年检状态
            byId.setInspectionStatus("1");
            //添加负责人
            byId.setHeadMan(sysUser.getUserName());
            byId.setHeadUserId(sysUser.getUserId());
            //年检启动时间
            byId.setStartTime(new Date());
            annualInspectionService.updateById(byId);
        }catch (Exception e){
            e.printStackTrace();
            return error(Constants.UNVERIFY,"服务错误，请联系管理员 ！");
        }
        return success(byId);
    }

    /**
     * 确认完成电梯年检项目
     * @param token
     * @param annualInspection
     * @return
     */
    @PostMapping("/confirmAnnualInspection")
    @ResponseBody
    public Map<String,Object> confirmAnnualInspection(String token,AnnualInspection annualInspection){

        if(StringUtils.isNull(annualInspection.getInspectionId())){
            return error(Constants.UNVERIFY,"缺少必传参数：id !");
        }

        if(StringUtils.isEmpty(annualInspection.getInspectionContext())){
            return error(Constants.UNVERIFY,"请填写年检描述 !");
        }

        if(StringUtils.isEmpty(annualInspection.getInspectionImg())){
            return error(Constants.UNVERIFY,"请上传年检图片 !");
        }

        Long userId = getUserId(token);

        AnnualInspection byId = annualInspectionService.getById(annualInspection.getInspectionId());

        /**
         * 校验是否本人的单子
         */
        if(!byId.getHeadUserId().equals(userId)){
            return error(Constants.UNVERIFY,"您不是该年检的负责人，无法完成 ！");
        }

        try {
            //修改年检状态
            annualInspection.setInspectionStatus("2");
            //年检完成时间
            annualInspection.setCompletionTime(new Date());
            annualInspectionService.updateById(annualInspection);
        }catch (Exception e){
            e.printStackTrace();
            return error(Constants.UNVERIFY,"服务错误，请联系管理员 ！");
        }
        return success(annualInspection);
    }


    /**
     * 查询详情接口
     * @param inspectionId
     * @return
     */
    @GetMapping("/selectAnnualDetails")
    @ResponseBody
    public Map<String,Object> selectAnnualDetails(Long inspectionId){

        if(StringUtils.isNull(inspectionId)){
            return error(Constants.UNVERIFY,"缺少必要参数id");
        }

        //年检信息查询
        AnnualInspection byId = annualInspectionService.getById(inspectionId);

        AnnualPlan planById = annualPlanService.getById(byId.getPlanId());

        //所属电梯信息查询
        ElevatorInfo elevator = elevatorInfoService.getById(planById.getElevatorId());


        JSONObject jsonObject = new JSONObject();

        jsonObject.put("elevator",elevator);
        jsonObject.put("inspection",byId);
        jsonObject.put("user",userService.selectUserById(byId.getHeadUserId()));
        return success(jsonObject);
    }

}
